package com.dao.order.pets;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.hibernate.sql.Delete;
import org.springframework.stereotype.Repository;

import com.DBUtil;

import com.entity.Admins;
import com.entity.Page;
import com.entity.Pets;
import com.entity.User;
import com.sun.org.apache.xpath.internal.operations.And;

import jdk.nashorn.internal.objects.annotations.Where;



@Repository
public class PetsDaoImpl implements PetsDao {
	@Resource
	private SessionFactory sessionFactory;
	private Session getSession(){
		return sessionFactory.getCurrentSession();
	}

	@Override
	public List<Pets> findAll() {
		String hql="from Pets";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		List<Pets> list=query.list();
		System.out.println(list+"dao");
		return list;
		
	}

	@Override
	public boolean savaPets(Pets pets) {
		return (boolean) getSession().save(pets);
	}

	@Override
	public boolean deletePets(int id) { 
		Transaction tran=getSession().beginTransaction();
		String hql="delete from Pets where id=?";
		Query<?> q=getSession().createQuery(hql);
		q.setParameter(0,id);
		q.executeUpdate();
		tran.commit();
		return true;
	}

	@Override
	public boolean updatePets(Pets pets) {
		String hql="update Pets pets set pets.name=:name,pets.sex=:sex,pets.type=:type,pets.describe=:describe,where pets.id=:id";
		Query<?> query=getSession().createQuery(hql);
		query.setParameter("id",pets.getId());
		query.setParameter("name",pets.getName());
		query.setParameter("sex",pets.getSex());
		query.setParameter("type",pets.getType());
		query.setParameter("describe",pets.getDescribe());
		return true ;
	}

	@Override
	public Pets selectPets(int id) {
		String hql = "from Pets where id=?";
		Query<?> query = getSession().createQuery(hql);
		query.setParameter(0, id);
		
		Pets pets=(Pets) query.uniqueResult();
		System.out.println(pets);
		return pets;
	}
	@Override
	public List<Pets> findPetsByPage(int pageName, int pageSize) {
		try{
			List<Pets> list=new ArrayList<Pets>();
			Connection con=DBUtil.getCon();
			//MySQL数据库中使用limit关键字进行分页，它有两个参数，第一个参数是起始位置，第二个参数的每页显示几条
			PreparedStatement pstm=con.prepareStatement("select * from pets pic limit ?,?");//查询数据库
			pstm.setInt(1, (pageName-1)*pageSize);
			pstm.setInt(2, pageSize);
			ResultSet rs=pstm.executeQuery();//返回结果集
			while(rs.next()){
				Pets p=new Pets();
				p.setId(rs.getInt(1));
				p.setName(rs.getString(2));
				p.setSex(rs.getString(3));
				p.setType(rs.getString(4));
				p.setPic(rs.getString(5));
				p.setDescribe(rs.getString(6));
				p.setPrice(rs.getInt(7));
				p.setDiscountprice(rs.getInt(8));
				list.add(p);
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public int findCount() {
		int result=0;
		try{
			
			Connection con=DBUtil.getCon();
			PreparedStatement pstm=con.prepareStatement("select count(*) from Pets");//查询数据库
			ResultSet rs=pstm.executeQuery();//返回结果集
			while(rs.next()){
				 result=rs.getInt(1);
			}
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}	
	@Override
	public int subPrice(int id,int account){
		int result=0;
		try{
			Session session=getSession();
			Query query = session.createQuery("update from User u set u.account=:account where u.id=:id");
			query.setParameter("account", account);
			query.setParameter("id", id);
			result = query.executeUpdate();
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public User user(int id) {
		Session session=getSession();
		User user = session.get(User.class, id);
		System.out.println("user:"+user.getAccount());
		return user;
	}

	@Override
	public Admins admin(int id) {
		Session session=getSession();
		String hql="from Admins  where id=?";
		Query query=session.createQuery(hql);
		query.setParameter(0, id);
		Admins admin =(Admins) query.uniqueResult();
		System.out.println(admin+"dao层");
		//Admin admin = session.get(Admin.class, id);
		return admin;
	}

	@Override
	public void updateAdmin(Admins admin) {
		Session session=getSession();
		session.update(admin);
		
	}	
}

